<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <style>
        .sidebar{
            background: #eee;
            color: deepskyblue;
            font-size: 14px;
        }
        .sidebar li{
            border-bottom:1px solid gray;
        }
        .sidebar .hidemenu ul{
            display: none;
        }
        .sidebar .showmenu ul{
            display: block;
        }
    </style>
</head>
<body>
    <ul class="sidebar">
        <li  class="showmenu">
                <a href="javacript:void">这是菜单1</a>
            <ul>
                <li>这是子菜单1</li>
                <li>这是子菜单2</li>
            </ul>


        </li>
        <li class="hidemenu">
                <a href="javacript:void">这是菜单2</a>
                <ul>
                        <li>这是子菜单3</li>
                        <li>这是子菜单4</li>
                    </ul>
        </li>
        
    </ul>
    <script>

        $(document).ready(function() {
            console.log($(".sidebar>li>a"))
            $(".sidebar>li>a").click(function(ele){
                $(".sidebar>li").each(function() {
                   if($(this).hasClass("hidemenu"))return;
                   if($(this).hasClass("showmenu")){
                    $(this).removeClass("showmenu");
                    $(this).addClass("hidemenu");
                   }else{
                    $(this).addClass("hidemenu");
                   }
                });
                $(this).parent().removeClass("hidemenu");
                $(this).parent().addClass("showmenu");
                ele.preventDefault();
            })
	    });
        
        
    </script>
</body>
</html>